package com.tools.app.ui.activity;

import android.os.Bundle;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.gms.ads.AdView;
import com.hm.app.R;
import com.hm.base.android.mob.AMApplication;
import com.hm.base.android.ui.util.ScreenUtil;
import com.tools.ad.ADConstant;
import com.tools.ad.EntranceType;
import com.tools.app.AppApplication;
import com.tools.cleanmaster.ui.base.SingleActivity;

/**
 * desc:
 * date: 2018/5/2
 * author: ancun
 */

public class AutoCleanActivity extends SingleActivity implements View.OnClickListener {

    protected AppApplication imContext;

    private ViewGroup mAdContainerFather;
    private ViewGroup mAdContainer;
    private ImageView mClose;
    private View mAdView;

    private TextView mAutoCleanDesc;
    private TextView mTotalCleanNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imContext = (AppApplication) AMApplication.getInstance();
        handleExtra();
        setContentView(R.layout.activity_auto_clean);
        overridePendingTransition(R.anim.fade_in, 0);
        init();
    }

    public void handleExtra() {
        if (getIntent() != null) {
            int adType = getIntent().getIntExtra(ADConstant.AD_TYPE, 0);
        }
    }

    private void init() {
        initView();
        initAdView();
        initListener();
        initDesc();
    }

    private void initView() {
        mAdContainer = (ViewGroup) findViewById(R.id.ad_container);
        mAdContainerFather = (ViewGroup) findViewById(R.id.ad_container_father);
        mClose = (ImageView) findViewById(R.id.close);
        mAutoCleanDesc = findViewById(R.id.auto_clean_desc);
        mTotalCleanNum = findViewById(R.id.auto_clean_num);
    }

    private void initDesc() {
        mAutoCleanDesc.setText(getString(R.string.auto_clean_desc));

        long lastCleanSize = imContext.getAutoCleanManager().getLastCleanTotalSize();
        if (lastCleanSize > 0) {
            mTotalCleanNum.setVisibility(View.VISIBLE);
            mTotalCleanNum.setText(Formatter.formatFileSize(this, lastCleanSize));
            mAutoCleanDesc.setText(R.string.auto_clean_desc);
        } else {
            mTotalCleanNum.setVisibility(View.GONE);
            mAutoCleanDesc.setText(R.string.auto_clean_null_desc);
        }
    }

    private void initAdView() {
        mAdView = imContext.getADModule().getAdSceneManager().getAdView(EntranceType.AUTO_CLEAN);

        //卸载场景 , 如果是admob banner 重设一下padding
        if (mAdView instanceof AdView) {
            mAdContainerFather.setPadding(0, ScreenUtil.dp2px(this, 9), 0, ScreenUtil.dp2px(this, 9));
        }

        if (mAdView != null) {
            mAdContainer.removeAllViews();
            mAdContainer.addView(mAdView);
        }
    }

    private void initListener() {
        mClose.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAdContainer.removeAllViews();
        mAdView = null;
        imContext.getADModule().getAdSceneManager().release(EntranceType.AUTO_CLEAN);

        imContext.getADModule().getAdSceneManager().load(EntranceType.AUTO_CLEAN_DEFER);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.fade_out);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case com.hm.app.R.id.close:
                if (!isFinishing()) {
                    finish();
                }
                break;

            default:
                break;
        }
    }


}
